var monitroingData,
listTpl = enco.util.createTemplate(
	'<tr>' +
	'<td>{index}</td>' +
	'<td>{ymdt}</td>' +
	'<td>{icnt}</td>' +
	'<td>{ccnt}</td>' +
	'<td>{dcnt}</td>' +
	'<td>{ncnt}</td>' +
	'{tot}' + 
	'</tr>',
	'<tbody>'
),
noListTpl = enco.util.createTemplate(
	'<tr>' +
	'<td colspan="6">검색된 데이터가 없습니다.</td>' +
	'</tr>',
	'<tbody>'
);

$(document).ready(function(){
	$("#loading").ajaxStart(function(){
	    $.blockUI({ 
	        message: $(this), 
	        css: { 
	            top:  ($(window).height() - 100) /2 + 'px', 
	            left: ($(window).width() - 400) /2 + 'px', 
	            width: '400px' 
	        } 
	    });
	}).ajaxComplete(function(){
		 setTimeout($.unblockUI, 100);
	});
	
	//calendar
	$('.date-pick').datePicker({autoFocusNextInput: true});
	$(".date-pick").mask("9999-99-99");	//Date type masking
});

function doSearch(page) 
{
	var startDate = $("#startDate").val(),
	    endDate = $("#endDate").val(),
	    productNo = $("#productNo").val(),
	    bbsTypeCd = $("#bbsTypeCd").val();
	
	if(startDate == '' || endDate == '') {
		alert("기간을 선택해 주세요.");
		return false;
	}
	
	if(productNo == '') {
		alert("서비스를 선택해 주세요.");
		return false;
	}
	
	if(bbsTypeCd == '') {
		alert("게시판 종류를 선택해 주세요.");
		return false;
	}
	
	var params = {
		startDate: startDate,
		endDate: endDate,
		productNo: productNo,
		bbsTypeCd: bbsTypeCd,
	};
	
	$.get("/bbs/statistics/search", params, function(result) {
		monitoringData = JSON.parse(result).monitoringList;
		var len = monitoringData.length;
		var total = JSON.parse(result).total;
		var target = document.getElementById("monitoringList");
		var last = len - 1, totIcnt = 0, totDcnt = 0, totCcnt = 0, totNcnt = 0;
		enco.log("len = " + len);
		
		if(len > 0) {
			for(var i=0;i<len;++i) {
				monitoringData[i].index = (i + 1);
				monitoringData[i].tot = '';
				totIcnt += monitoringData[i].icnt;
				totDcnt += monitoringData[i].dcnt;
				totCcnt += monitoringData[i].ccnt;
				totNcnt += monitoringData[i].ncnt;
				
				if(i == last) {
					monitoringData[i].tot =
						'</tr><tr>' +
						'<th colspan="2">합계</th>' +
						'<th>' + totIcnt + '</th>' +
						'<th>' + totDcnt + '</th>' +
						'<th>' + totCcnt + '</th>' +
						'<th>' + totNcnt + '</th>';
				}
				listTpl.append(null, monitoringData[i]);
			}
			listTpl.flush(target, "overwrite");
		} else {
			noListTpl.overwrite(target, null);
		}
		
		$("#total").text(total);
	});

	return false;
}